import 'dart:ui';
import 'package:flutter_bmfmap_example/models/marketinfomodel.dart';
import 'package:flutter_bmfmap_example/models/shoppingCart.dart';
import 'package:flutter_bmfmap_example/pages/marketpage/pages/submitOrders/submitOrderspage.dart';
import 'package:provider/provider.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bmfmap_example/http/http.dart';

import 'buyCarValue/buyCarValue.dart';

class BuycarPage extends StatefulWidget {
  BuycarPage({Key key}) : super(key: key);

  @override
  _BuycarPageState createState() => _BuycarPageState();
}

class _BuycarPageState extends State<BuycarPage> {
  bool shoppingCartSelectionAll = false;

  @override
  initState() {
    // TODO: implement initState
    super.initState();
    // 页面加载时调用 用来开始时访问数据库
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        leading: InkWell(
          onTap: () {
            Navigator.pop(context);
          },
          child: Padding(
            padding: const EdgeInsets.all(8.0),
            child: Container(
              decoration: BoxDecoration(
                  color: Colors.white,
                  border: Border.all(color: Colors.grey[200], width: 0.5),
                  borderRadius: BorderRadius.circular(10),
                  boxShadow: [
                    BoxShadow(
                        color: Colors.black12,
                        blurRadius: 4,
                        spreadRadius: 0,
                        offset: Offset(0, 0))
                  ]),
              child: Icon(
                Icons.arrow_back,
                size: 18,
                color: Colors.blueGrey[800],
              ),
            ),
          ),
        ),
        centerTitle: true,
        title: Text(
          '购物车',
          style: TextStyle(color: Colors.black),
        ),
        backgroundColor: Colors.transparent,
        elevation: 0,
      ),
      body: SafeArea(
        child: Container(
          // color: Colors.grey[100],
          child: Column(
            children: [
              Expanded(
                child: ListView(
                  physics: const AlwaysScrollableScrollPhysics(
                    parent: BouncingScrollPhysics(),
                  ),
                  children: _listCart(),
                ),
              ),
            ],
          ),
        ),
      ),
      bottomNavigationBar: Container(
        height: 160,
        color: Colors.grey[50],
        child: Container(
          padding: EdgeInsets.fromLTRB(16, 24, 16, 24),
          child: Column(
            children: [
              Container(
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Container(
                      margin: EdgeInsets.only(right: 8),
                      width: 18,
                      child: Theme(
                        data: ThemeData(unselectedWidgetColor: Colors.black26),
                        child: Checkbox(
                          shape: CircleBorder(), //这里就是实现圆形的设置
                          value: context
                              .watch<Shoppingcatmodel>()
                              .shoppingcartselectall,
                          onChanged: (bool value) {
                            context.read<Shoppingcatmodel>().changeselectall();
                          },
                        ),
                      ),
                    ),
                    Text(
                      '总价(${_getCartNum()} 个商品):',
                      style: TextStyle(
                          fontWeight: FontWeight.w500,
                          color: Colors.blueGrey[500],
                          fontSize: 18),
                    ),
                    Spacer(),
                    Text(
                      '${_totalprice()}',
                      style: TextStyle(
                          fontWeight: FontWeight.w500,
                          color: Colors.blueGrey[800],
                          fontSize: 18),
                    )
                  ],
                ),
              ),
              InkWell(
                onTap: () {
                  _gotobuy();
                },
                child: Container(
                  height: 44,
                  // margin: EdgeInsets.fromLTRB(32, 0, 32, 0),
                  // padding: EdgeInsets.fromLTRB(32, 10, 32, 10),
                  decoration: BoxDecoration(
                      // color: Colors.blueAccent[400],
                      borderRadius: BorderRadius.circular(10),
                      boxShadow: [
                        BoxShadow(
                            color: Colors.grey[400],
                            blurRadius: 40,
                            spreadRadius: 1,
                            offset: Offset(2, 8))
                      ],
                      gradient: LinearGradient(
                          begin: Alignment.topRight, end: Alignment.bottomLeft,
                          // stops: [0.0,1.0],
                          colors: [Colors.blue[800], Colors.green[200]])),
                  child: Padding(
                    padding: const EdgeInsets.only(left: 16.0, right: 16),
                    child: Row(
                      // mainAxisAlignment: MainAxisAlignment.spaceAround,
                      children: [
                        Text(
                          'Immediate payment',
                          style: TextStyle(
                              fontWeight: FontWeight.w500,
                              color: Colors.white,
                              fontSize: 18),
                        ),
                        Spacer(),
                        Container(
                          child: Icon(
                            Icons.keyboard_arrow_right_outlined,
                            color: Colors.white,
                            size: 24,
                          ),
                        )
                      ],
                    ),
                  ),
                ),
              )
            ],
          ),
        ),
      ),
    );
  }

  _gotobuy() {
    String content = '';

    context.read<Shoppingcatmodel>().getshoppingcartMap().forEach((key, value) {
      content += value.commodityId.toString() +
          "x" +
          value.commodityNumber.toString() +
          ";";
    });

    Navigator.push(
      context,
      MaterialPageRoute(
        builder: (context) => SubmitOrdersPage(
          content: content,
          money: _totalprice(),
        ),
      ),
    );
  }

  _listCart() {
    List<buyCarValue> list = [];
    context
        .watch<Shoppingcatmodel>()
        .getshoppingcartMap()
        .forEach((int key, Shoppingcart value) {
      list.add(buyCarValue(
        id: value.id,
        name: value.name,
        intro: value.intro,
        money: value.money,
        commodityNumber: value.commodityNumber,
        imgurl1: value.imgurl1,
        Selection: value.selection,
      ));
    });
    return list;
  }

  // subCart(int id) {
  //   if (this.buyCarData[id]['commodityNumber'] == 1) {
  //     return;
  //   }
  //   this.buyCarData[id]['commodityNumber'] -= 1;
  //   setState(() {});
  // }

  // addCart(int id) {
  //   this.buyCarData[id]['commodityNumber'] += 1;
  //   setState(() {});
  // }

  // removeCart(int id) {
  //   this.buyCarData.removeAt(id);
  //   // print('删除');
  //   // print(this.buyCarData);
  //   this.shoppingCartSelection.removeAt(id);
  //   setState(() {});
  // }

  _getCartNum() {
    return context.watch<Shoppingcatmodel>().getshoppingcatselectnum();
  }

  _totalprice() {
    return context.watch<Shoppingcatmodel>().getshoppingcattotalprice();
  }
}
